<template>
  <page-layout>
    <div slot="toolbar">
      <el-button size="small" round type="primary" @click="newGift">新增礼品</el-button>
    </div>
    <div class="card-header">礼品管理</div>
    <el-card class="box-card">
      <el-card class="box-card">
      <el-row>
        <el-col :span="24">
            <el-form ref="searchGiftCondition" :model="searchGiftCondition" label-width="80px" inline>
              <el-form-item label="商品名称">
                <el-input v-model="searchGiftCondition.title" style="width: 400px;"/>
              </el-form-item>
              <el-form-item label="状态">
                <el-select v-model="searchGiftCondition.status" placeholder="请选择" style="width: 400px;">
                  <el-option
                    v-for="item in GiftStatus"
                    :key="item.value"
                    :label="item.text"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="search(1)">查询</el-button>
              </el-form-item>
            </el-form>
          </el-col>
        </el-row>
      </el-card>
      <el-card class="box-card">
            <el-table
              :data="giftList.content" border stripe
              style="width: 100%">
              <el-table-column
                prop="id"
                label="编号"
                width="55">
              </el-table-column>

              <el-table-column prop="title" label="商品名称" ></el-table-column>
              <el-table-column prop="category" label="分类" width="80" ></el-table-column>
              <el-table-column
                prop="orgPrice"
                label="原价" width="80">
              </el-table-column>
              <el-table-column prop="slide" label="缩略图" width="110">
                <template slot-scope="scope">
                  <image-cover v-if="scope.row.slide[0]" :thumb.sync="scope.row.slide[0]" width="96px" height="63px" size-type="contain"></image-cover>
                  <span v-else>暂时无图</span>
                </template>
              </el-table-column>
              <el-table-column
                prop="store"
                label="兑换积分" width="120">
              </el-table-column>
              <el-table-column
                label="已兑换" width="80">
                <template slot-scope="scope">
                  <span>{{scope.row.verifyCount+scope.row.unVerifyCount}}</span>
                </template>
              </el-table-column>

              <el-table-column
                prop="verifyCount"
                label="已领取" width="80">
              </el-table-column>
              <el-table-column
                prop="unVerifyCount"
                label="待领取" width="80">
              </el-table-column>
              <el-table-column
                prop="repertory"
                label="剩余库存" width="80">
              </el-table-column>
              <el-table-column
                prop="status"
                label="状态" width="120">
                <template slot-scope="scope">
                  <span>{{scope.row.status| keyText('GiftStatus')}}</span>
                </template>
              </el-table-column>
              <el-table-column label="操作" width="180">
                <template slot-scope="scope">
                  <!--<el-button size="mini" type="text"  @click="preGift(scope.row)"> 预览 </el-button>-->
                  <el-button size="mini" v-if="scope.row.status==='EDIT'||scope.row.status==='OUTSALE'" @click="editGift(scope.$index, scope.row)" type="text">编辑</el-button>
                  <el-button  size="mini" type="text"  v-if="scope.row.status==='EDIT'||scope.row.status==='ONSALE'"  @click="handleTop(scope.row)">置顶</el-button>
                  <el-button  size="mini" type="text"  v-if="scope.row.status==='EDIT'"  @click="save(scope.row,'ONSALE')">发布</el-button>
                  <el-button  size="mini" type="text"  v-if="scope.row.status==='ONSALE'"  @click="save(scope.row,'OUTSALE')">下架</el-button>
                  <el-button size="mini" type="text" @click="deleteGift(scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <div class="mt10">
              <el-pagination
                background
                layout="prev, pager, next"
                :total="giftList.totalElements"
                :page-size="giftList.size"
                :current-page.sync="currentGiftPage" @current-change="getPageChange">
              </el-pagination>
            </div>
          </el-card>
    </el-card>
    <el-dialog :visible.sync="showGiftEditor" title="添加/编辑 礼品" width="80%">
      <gift-editor @close="onGiftEditorClose" :is-close="showGiftEditor"></gift-editor>
    </el-dialog>
    <el-dialog title="预览" :visible.sync="showPreview" width="50%">
      <pre-gift @close="onShowPreviewClose"  :is-close="showPreview"></pre-gift>
    </el-dialog>


  </page-layout>
</template>
<script>
import PageLayout from "@/components/page-layout";
import GiftEditor from "./gift-editor";
import { mapGetters } from "vuex";
import ImageCover from "@/components/image-cover";
import PreGift from "./pre-gift";
import Status from "@/libs/status";
import Moment from "moment";
import _ from "lodash";
export default {
  created() {
    this.search(1);
  },
  mounted() {
    this.GiftStatus = _.clone(Status.GiftStatus);
    this.GiftStatus.unshift({ value: "", text: "全部" });
  },
  data() {
    return {
      currentGiftPage: 1,
      showGiftEditor: false,
      showPreview: false,
      curPreObj: {},
      GiftStatus: [],
      value4: [],
      searchGiftCondition: {
        pageSize: 10,
        title: "",
        status: ""
      }
    };
  },
  computed: {
    ...mapGetters({
      giftList: "giftList"
    })
  },
  methods: {
    seeDiscuss(row) {
      this.$router.push({
        name: "discuss",
        query: { giftId: row.id }
      });
    },
    getPageChange(val) {
      this.searchGiftCondition.startTime = "";
      this.searchGiftCondition.endTime = "";
      if (this.value4 && this.value4.length > 0) {
        this.searchGiftCondition.startTime = Moment(this.value4[0]).format(
          "YYYY-MM-DD HH:mm:ss"
        );
        this.searchGiftCondition.endTime = Moment(this.value4[1]).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
      console.log(this.searchGiftCondition);
      var tem = Object.assign(this.searchGiftCondition, { pageIndex: val - 1 });
      console.log(tem);
      this.$store.dispatch("getGiftPageS", tem).then(ret => {});
    },
    search(val) {
      this.getPageChange(val);
    },
    deleteGift(row) {
      this.$confirm("确认删除当前新闻?", "提示", {})
        .then(() => {
          this.$store.dispatch("deleteGiftS", { id: row.id }).then(ret => {
            this.getPageChange(this.currentGiftPage);
            this.$notify({
              title: "删除成功",
              type: "success"
            });
          });
        })
        .catch(() => {});
    },
    handleTop(row) {
      this.$store.dispatch("topGiftS", row.id).then(ret => {
        this.getPageChange(this.currentGiftPage);
      });
    },
    preGift(row) {
      console.log(row.id);
      this.$store.dispatch("getGiftS", row.id).then(ret => {});
      this.showPreview = true;
    },
    newGift() {
      this.$store.commit("INIT_CURRENT_GIFT");
      this.showGiftEditor = true;
    },
    editGift(index, row) {
      this.$store.dispatch("getGiftS", row.id).then(ret => {});
      this.showGiftEditor = true;
    },
    onGiftEditorClose() {
      this.showGiftEditor = false;
      this.getPageChange(this.currentGiftPage);
    },
    save(row, status) {
      row.status = status;
      this.$store.dispatch("addOrUpdateGiftS", row).then(ret => {
        this.$notify({
          title: "提交成功",
          type: "success"
        });
        this.getPageChange(this.currentGiftPage);
      });
    },
    onShowPreviewClose() {
      this.showPreview = false;
      this.getPageChange(this.currentGiftPage);
    }
  },
  components: {
    PageLayout,
    GiftEditor,
    ImageCover,
    PreGift
  }
};
</script>
<style>
</style>
